<template>
  <div class="login-body">
    <div class="container">
      <form class="form-signin" action="index.html">
        <div class="form-signin-heading text-center">
          <h1 class="sign-title">胖虎记账本</h1>
          <h2 style="color:#c9d4dd;font-family:'Microsoft YaHei'">LOGIN</h2>
        </div>
        <el-form ref="form" :model="userInfo" class="login-wrap">
          <el-form-item >
            <el-input v-model="userInfo.name" placeholder="账号" clearable autofocus></el-input>
          </el-form-item>
          <el-form-item >
            <el-input placeholder="密码" v-model="userInfo.password" show-password clearable></el-input>
          </el-form-item>
          <el-button type="primary" class="btn btn-lg btn-login btn-block" @click="login">
            <i class="el-icon-check"></i>
          </el-button>
          <div class="registration">
            Have a member yet?
            <a class="" href="/register">Register</a>
          </div>
        </el-form>
      </form>
    </div>
    <Footer style="margin: 16% auto;"></Footer>
  </div>
</template>

<script>
import {setToken} from "@/plugins/token";
import {saveUser} from "@/api/user";
import dayjs from "dayjs";
import Footer from '@/components/Footer'

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",
  components:{
    Footer,
  },
  data() {
    return {
      userInfo:{
        name: '',
        password: '',
      },
      user:{},
    }
  },
  methods:{
    //登录点击事件回调,验证登录信息
    async login(){
      //派发登录actions
      const verifyLogin=await this.$API.default.user.login(this.userInfo);
      this.user=verifyLogin;
      if(verifyLogin=='登录成功'){
        setToken(this.userInfo.name)
        //派发dispatch
        this.$store.dispatch('saveUser', this.userInfo)
        /*由于lastlogintime=this.$store.user.user.lastlogintime;语句走完时，vuex中的数据还没更新，lastlogintime取到的始终是旧数据
        所以直接发请求获取lastlogintime参数*/
        const result=await saveUser(this.userInfo);
        const lastlogintime=result.lastlogintime;
        this.$router.push({path:'/home'})
        this.$nextTick(()=>{
          this.$message({
            type:'success',
            message: "上次登录："+dayjs(lastlogintime).format("YYYY-MM-DD HH:mm:ss"),
            duration:4000
          })
        })
      }
      else if(verifyLogin=='用户名不存在!'){
        this.$message({
          type:'warning',
          message:verifyLogin,
          duration:2000,
        })
      }else if(verifyLogin=='密码错误！'){
        this.$message({
          type:'error',
          message:verifyLogin,
          duration:2000,
        })
      }
    },
  }
}
</script>

<style scoped>
.login-body {
  /*background: #42b983 ;*/
  background: #65cea7 url("~@/assets/images/csgo.jpg");
  background-size: cover;
  padding-top: 1px;
  width: 100%;
  height: 100vh;
}
.container {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px
}

@media (min-width: 768px) {
  .container {
    width: 750px
  }
}

@media (min-width: 992px) {
  .container {
    width: 970px
  }
}

@media (min-width: 1200px) {
  .container {
    width: 1170px
  }
}
.form-signin {
  max-width: 330px;
  margin: 100px auto;
  /*background: #29d;*/
  background: #fff;
  border-radius: 5px;
  -webkit-border-radius: 5px;
}

.form-signin .form-signin-heading {
  margin: 0;
  padding: 10px 15px;
  text-align: center;
  color: #fff;
  position: relative;
}

.sign-title {
  font-size: 24px;
  color: #fff;
  position: absolute;
  top: -60px;
  left: 0;
  text-align: center;
  width: 100%;
  text-transform: uppercase;
}


.form-signin .checkbox a, .form-signin .checkbox a:hover {
  color: #fff;
}


.form-signin input[type="text"], .form-signin input[type="password"] {
  margin-bottom: 15px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  border: 1px solid #eaeaec;
  background: #eaeaec;
  box-shadow: none;
  font-size: 12px;
}

.form-signin .btn-login {
  background: #6bc5a4;
  color: #fff;
  text-transform: uppercase;
  font-weight: normal;
  font-family: 'Open Sans', sans-serif;
  margin: 20px 0 5px;
  padding: 5px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
  font-size: 30px;
}

.form-signin .btn-login:hover {
  background: #2ee09c;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}

.form-signin p {
  text-align: left;
  color: #b6b6b6;
  font-size: 16px;
  font-weight: normal;
}

.form-signin a, .form-signin a:hover {
  color: #6bc5a4;
}

.form-signin a:hover {
  text-decoration: underline;
}

.login-wrap {
  padding: 15px 25px;
  position: relative;
}
.btn {
  display: inline-block;
  margin-bottom: 0;
  font-weight: 400;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 4px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none
}
.btn-lg, .btn-group-lg > .btn {
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.33;
  border-radius: 6px
}
.btn-block {
  display: block;
  width: 100%;
  padding-left: 0;
  padding-right: 0
}

.btn:hover, .btn:focus {
  color: #333;
  text-decoration: none
}

.registration {
  color: #c7c7c7;
  text-align: center;
  margin-top: 15px;
}
</style>